package com.example.mymnue;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class HistoryActivity extends AppCompatActivity implements Runnable {
    private static final String TAG= "AA";
    private ShopDB db;
    private SimpleAdapter simpleAdapter;
    private List<HashMap<String, Object>> listItems; // 存放文字、图片信息
    private HashMap<String,Object> map;
    private Handler handler;
    private List<CaidanItem> caidanItemList;
    private CaidanDBManager cd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ListView slist=findViewById(R.id.hlist);
        initListView();
        slist.setAdapter(simpleAdapter);
        Thread thread=new Thread((Runnable) this);
        thread.start();
        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 7){
                    listItems =(List<HashMap<String, Object>>) msg.obj;
                    simpleAdapter=new SimpleAdapter(HistoryActivity.this,listItems,
                            R.layout.list_item_shop,
                            new String[]{"title","detail"},
                            new int[]{R.id.textView13,R.id.textView14});
                    slist.setAdapter(simpleAdapter);
                }
                super.handleMessage(msg);
            }
        };

    }
    private void initListView() {
        listItems = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            map = new HashMap<String, Object>();
            map.put("title",i);
            map.put("detail", i); // 详情描述
            listItems.add(map);
        }
        // 生成适配器的Item和动态数组对应的元素
        simpleAdapter = new SimpleAdapter(this, listItems, // listItems数据源
                R.layout.list_item_shop, // ListItem的XML布局实现
                new String[] {"title","detail"},
                new int[] {  R.id.textView13,R.id.textView14 }
        );
    }
    @Override
    public void run() {
        db=new ShopDB(this,"mymenu.db",null,1);
        listItems=new ArrayList<HashMap<String,Object>>();
        SQLiteDatabase sd=db.getWritableDatabase();
        String curDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
        cd=new CaidanDBManager(this);
        caidanItemList = cd.list();
        Log.i(TAG, "run: caidan  "+caidanItemList+"="+caidanItemList.size());
        for (int i=0;i<caidanItemList.size();i++){
                map=new HashMap<String,Object>();
                map.put("title",caidanItemList.get(i).getDtitle());
                map.put("detail",caidanItemList.get(i).getDdtail());
                listItems.add(map);

        }
        Message msg=new Message();
        msg.what=7;
        msg.obj=listItems;
        handler.sendMessage(msg);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.backmenu,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.back:
                Intent intent1=new Intent(HistoryActivity.this,MainActivity.class);
                startActivity(intent1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}